package com.bootdo.fx.service.impl;

import com.bootdo.common.utils.IdWorker;
import com.bootdo.fx.dao.UserFundsInfoDao;
import com.bootdo.fx.domain.UserFundsInfoDO;
import com.bootdo.fx.service.UserFundsInfoService;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.fx.dao.UserInfoDao;
import com.bootdo.fx.domain.UserInfoDO;
import com.bootdo.fx.service.UserInfoService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class UserInfoServiceImpl implements UserInfoService {
	@Autowired
	private UserInfoDao userInfoDao;

	@Autowired
	private IdWorker idWorker;

	@Autowired
	private UserFundsInfoDao userFundsInfoDao;
	
	@Override
	public UserInfoDO get(Long userId){
		return userInfoDao.get(userId);
	}
	
	@Override
	public List<UserInfoDO> list(Map<String, Object> map){
		return userInfoDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return userInfoDao.count(map);
	}
	
	@Override
	public int save(UserInfoDO userInfo){
		return userInfoDao.save(userInfo);
	}
	
	@Override
	public int update(UserInfoDO userInfo){
		return userInfoDao.update(userInfo);
	}
	
	@Override
	public int remove(Long userId){
		return userInfoDao.remove(userId);
	}
	
	@Override
	public int batchRemove(Long[] userIds){
		return userInfoDao.batchRemove(userIds);
	}

	@Override
	@Transactional
	public int saveUserAndFundsInfo(UserInfoDO userInfoDO) {
		int num = userInfoDao.save(userInfoDO);
		int n = 0;
		if(num > 0){

			if(userInfoDO.getUpUserId() != 0){
				//更新一级会员用户数量
				Map<String,Object> map = new HashMap<>();
				map.put("userId",userInfoDO.getUpUserId());
				UserFundsInfoDO u = userFundsInfoDao.list(map).get(0);
				if(u != null){
					u.setFundsFirstLevelCount(u.getFundsFirstLevelCount()+1);
					userFundsInfoDao.update(u);
				}
				//更新二级会员用户数量
				UserInfoDO ud = userInfoDao.get(userInfoDO.getUpUserId());
				if(ud != null && ud.getUpUserId() != 0){
					map.put("userId",ud.getUpUserId());
					UserFundsInfoDO u2 = userFundsInfoDao.list(map).get(0);
					if(u2 != null){
						u2.setFundsSecondLevelCount(u.getFundsSecondLevelCount()+1);
						userFundsInfoDao.update(u2);
					}
					//更新三级会员用户数量
					UserInfoDO  ud2= userInfoDao.get(ud.getUpUserId());
					if(ud2 != null && ud2.getUpUserId() != 0){
						map.put("userId",ud2.getUpUserId());
						UserFundsInfoDO u3 = userFundsInfoDao.list(map).get(0);
						if(u3 != null){
							u3.setFundsThreeLevelCount(u.getFundsThreeLevelCount()+1);
							userFundsInfoDao.update(u3);
						}
					}
				}
			}

			UserFundsInfoDO uf = new UserFundsInfoDO();
			uf.setFundsId(idWorker.nextId());
			uf.setUserId(String.valueOf(userInfoDO.getUserId()));
			//关系数---未知
			uf.setFundsGradeTree(null);

			uf.setFundsFirstLevelCount(0);
			uf.setFundsSecondLevelCount(0);
			uf.setFundsThreeLevelCount(0);
			uf.setFundsFirstSales(new BigDecimal(0));
			uf.setFundsSecondSales(new BigDecimal(0));
			uf.setFundsThirdSales(new BigDecimal(0));
			uf.setFundsSalesSummary(new BigDecimal(0));
			uf.setFundsDistributionCommission(new BigDecimal(0));
			uf.setFundsWithdrawMoney(new BigDecimal(0));
			uf.setFundsCreateTime(new Date());
			n = userFundsInfoDao.save(uf);
		}
		return n;
	}

	@Override
	public List<UserInfoDO> listByUp(String ids) {
		return userInfoDao.listByUp(ids);
	}

}
